<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% if action == 'new' %}新建任务{% else %}编辑任务{% endif %} - 简易任务管理系统</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
    <div class="container">
        <h1>{% if action == 'new' %}新建任务{% else %}编辑任务{% endif %}</h1>
        
        <!-- 消息提示区域 -->
        {% with messages = get_flashed_messages() %}
            {% if messages %}
                <div class="messages">
                    {% for message in messages %}
                        <div class="message">{{ message }}</div>
                    {% endfor %}
                </div>
            {% endif %}
        {% endwith %}
        
        <!-- 任务表单 -->
        <form class="task-form" method="POST">
            <div class="form-group">
                <label for="title">任务标题 *</label>
                <input type="text" id="title" name="title" value="{{ task[1] if task else '' }}" placeholder="请输入任务标题" required>
            </div>
            
            <div class="form-group">
                <label for="description">任务描述</label>
                <textarea id="description" name="description" placeholder="请输入任务描述">{{ task[2] if task else '' }}</textarea>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label for="status">任务状态</label>
                    <select id="status" name="status">
                        <option value="pending" {% if task and task[3] == 'pending' %}selected{% endif %}>待办</option>
                        <option value="in_progress" {% if task and task[3] == 'in_progress' %}selected{% endif %}>进行中</option>
                        <option value="completed" {% if task and task[3] == 'completed' %}selected{% endif %}>已完成</option>
                    </select>
                </div>
                
                <div class="form-group">
                    <label for="priority">任务优先级</label>
                    <select id="priority" name="priority">
                        <option value="high" {% if task and task[4] == 'high' %}selected{% elif not task %}selected{% endif %}>高</option>
                        <option value="medium" {% if task and task[4] == 'medium' %}selected{% endif %}>中</option>
                        <option value="low" {% if task and task[4] == 'low' %}selected{% endif %}>低</option>
                    </select>
                </div>
                
                <div class="form-group">
                    <label for="due_date">截止日期</label>
                    <input type="date" id="due_date" name="due_date" value="{{ task[5] if task else '' }}">
                </div>
            </div>
            
            {% if action == 'edit' and task %}
                <div class="task-meta">
                    <p><strong>创建时间:</strong> {{ task[6] }}</p>
                    <p><strong>更新时间:</strong> {{ task[7] }}</p>
                </div>
            {% endif %}
            
            <div class="form-actions">
                <button type="submit" class="save-btn">保存</button>
                <a href="{{ url_for('index') }}" class="cancel-btn">取消</a>
            </div>
        </form>
    </div>
    
    <script>
        // 设置默认截止日期为明天（新建任务时）
        document.addEventListener('DOMContentLoaded', function() {
            const action = '{{ action }}';
            const dueDateInput = document.getElementById('due_date');
            
            // 只有在新建任务且没有设置截止日期时才设置默认值
            if (action === 'new' && !dueDateInput.value) {
                const tomorrow = new Date();
                tomorrow.setDate(tomorrow.getDate() + 1);
                
                // 格式化为YYYY-MM-DD
                const formattedDate = tomorrow.toISOString().split('T')[0];
                dueDateInput.value = formattedDate;
            }
        });
    </script>
</body>
</html>